package com.company.eos.service.impl;

import com.company.eos.exception.EmosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.company.eos.common.util.PageUtils;
import com.company.eos.mapper.DeptMapper;
import com.company.eos.domain.Dept;
import com.company.eos.service.DeptService;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author: Song, Wei
 * @date: 2022-01-14
 */
@Service
@Slf4j
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public ArrayList<HashMap> searchAllDept() {
        return deptMapper.searchAllDept();
    }

    @Override
    public HashMap searchById(int id) {
        return deptMapper.searchById(id);
    }

    @Override
    public PageUtils searchDeptByPage(HashMap param) {
        ArrayList<HashMap> list = deptMapper.searchDeptByPage(param);
        long count = deptMapper.searchDeptCount(param);
        int start = (int) param.get("start");
        int length = (int) param.get("length");
        return new PageUtils(list, count, start, length);
    }

    @Override
    public int insert(Dept dept) {
        return deptMapper.insert(dept);
    }

    @Override
    public int update(Dept dept) {
        return deptMapper.update(dept);
    }

    @Override
    public int deleteDeptByIds(Integer[] ids) {
        if (!deptMapper.searchCanDelete(ids)) {
            throw new EmosException("无法删除关联用户的部门");
        }
        return deptMapper.deleteDeptByIds(ids);
    }
}
